package com.chinaoryx.dwr;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.beanutils.BasicDynaClass;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.type.TypeHandler;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class MyManager {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MyManager.class);


	
	public Collection<String> getStatementNames() throws IOException {
		String resource = "Configuration.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
		Configuration config = sqlMapper.getConfiguration();
		Collection<String> names = config.getMappedStatementNames();		
		return names;
	}
	
	
	
	
	
	public SqlStatement getStatementByName(String stmtName) throws IOException, DocumentException {
		String resource = "Configuration.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
		Configuration config = sqlMapper.getConfiguration();
		MappedStatement stmt = config.getMappedStatement(stmtName);
		SqlSource sqlSource = stmt.getSqlSource();
//		Collection<String> names = config.getMappedStatementNames();		
		BoundSql boundSql = sqlSource.getBoundSql(null);		
		List<SqlParameter> paramListNew = new ArrayList<SqlParameter>();
		
		SAXReader domreader = new SAXReader();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("paramType.xml");
        Document document = domreader.read(in);
        List list = document.selectNodes( "//config/sql[@id='"+stmtName+"']/parameter" );
        for (Object object : list) {
        	Element element = (Element) object;
			String name = element.attributeValue("name");
			String javaType = element.attributeValue("javaType");
			SqlParameter myParam = new SqlParameter();
			myParam.setParamName(name);
			myParam.setJavaType(javaType);
			paramListNew.add(myParam);
		}
		
//		List<ParameterMapping> paramList = boundSql.getParameterMappings();
//		if (paramList != null) {
//			for (ParameterMapping parameterMapping : paramList) {
//				String paramName = parameterMapping.getProperty();
//				String javaType = parameterMapping.getJavaType().getName();
//				TypeHandler handler = parameterMapping.getTypeHandler();
//				String handlerClassName = handler.getClass().toString();
//				SqlParameter myParam = new SqlParameter();
//				myParam.setJavaType(javaType);
//				myParam.setParamName(paramName);
//				myParam.setHandlerClassName(handlerClassName);
//				paramListNew.add(myParam);
//			}
//		}
		
		
		
		
		SqlStatement sqlStmt = new SqlStatement();
		sqlStmt.setSql(boundSql.getSql());
		sqlStmt.setParameterMappings(paramListNew);
		return sqlStmt;
	}
	
	public static class SqlStatement{
		private String sql;
		private List<SqlParameter> parameterMappings;
		
		public List<SqlParameter> getParameterMappings() {
			return parameterMappings;
		}
		public void setParameterMappings(List<SqlParameter> parameterMappings) {
			this.parameterMappings = parameterMappings;
		}
		public String getSql() {
			return sql;
		}
		public void setSql(String sql) {
			this.sql = sql;
		}
		
	}
	
	
	public static class SqlParameter {
		private String paramName;
		private String javaType;
		private String handlerClassName;
		public String getHandlerClassName() {
			return handlerClassName;
		}
		public void setHandlerClassName(String handlerClassName) {
			this.handlerClassName = handlerClassName;
		}
		public String getParamName() {
			return paramName;
		}
		public void setParamName(String paramName) {
			this.paramName = paramName;
		}
		public String getJavaType() {
			return javaType;
		}
		public void setJavaType(String javaType) {
			this.javaType = javaType;
		}
		
	}
	
	
	
	
	public static void main(String[] args) {
		logger.info("xxxxxxx");
	}

}
